#include "head.cuh"

int main(void){
	printf("Now: %s\n", UIviewFileName);

	Params params;
	params.split_rate = 0.8f;

	float *UIview = NULL;
	float *IUview = NULL;
	float *UIview_test = NULL;
	std::stringstream ss;
	float* d_UIview = NULL;
	float *d_IUview = NULL;

	cudaError_t cudaStatus = cudaSetDevice(0);
	if (cudaStatus != cudaSuccess) {
		fprintf(stderr, "cudaSetDevice failed!  Do you have a CUDA-capable GPU installed?");
		system("pause"); exit(1);
	}

	clock_t start_of_main = clock();

	////////////////////////////////////////////////////////////////////////////////////////
	//// UIview: User-Item rating matrix for training
	//// UIview_test: same dimension as UIview for testing score
	//// UUview: User-User friendship matrix which has all-ones diag and is symmetric
	////////////////////////////////////////////////////////////////////////////////////////
	
	UIview = (float*)malloc(nUsers*nItems*sizeof(float));
	CHECK_PARAM(UIview != NULL, "allocate memory space for UIview failed.");
	IUview = (float*)malloc(nItems*nUsers*sizeof(float));
	CHECK_PARAM(IUview != NULL, "allocate memory space for IUview failed.");
	UIview_test = (float*)malloc(nUsers*nItems*sizeof(float));
	CHECK_PARAM(UIview_test != NULL, "allocate memory space for UIview_test failed.");
	
	CHECK_PARAM(read_data_2_matrix(UIview, UIview_test, params.split_rate, IUview), "read data set failed.");
	
	ss.str("");
	ss << nUsers << "_" << nItems << "_" << "UIview_test.txt";
	write_test_data(UIview_test, nUsers, nItems, ss.str().c_str());
	free(UIview_test); UIview_test = NULL;

	clock_t end_of_read_dataset = clock();
	printf("read data set done. cost time:%.2f s\n", (double)(end_of_read_dataset - start_of_main) / CLOCKS_PER_SEC);

	params.maxS = 5;
	params.minS = 1;
	float hyper = 0.01;
	params.alpha = hyper;
	params.beta = hyper;


	std::string params_str;
	params_to_string(params, params_str);

	int x[5] = { 8, 16, 32, 64, 128 };
	for (int round = 0; round < 5; round++){//TODO
		printf("===================================\nround:%d %d bits\n", round, x[round]);
		params.r = x[round];

		float *B = (float *)malloc(params.r*nUsers*sizeof(float));
		CHECK_PARAM(B!=NULL,"allocate space for B failed.");
		float *D = (float *)malloc(params.r*nItems*sizeof(float));
		CHECK_PARAM(D!=NULL,"allocate space for D failed.");

		clock_t start_of_round = clock();
		DCF(B, D, params, UIview, IUview);

		clock_t end_of_dcf = clock();
		printf("dcf cost time %.2f\n", (double)(end_of_dcf - start_of_round) / CLOCKS_PER_SEC);


		ss.str("");
		ss << nUsers << "_" << nItems << "_" << params.r << "_" << params_str << "_" << "B_code.txt";
		write_into_file(B, params.r, nUsers, ss.str().c_str());

		ss.str("");
		ss << nUsers << "_" << nItems << "_" << params.r << "_" << params_str << "_" << "D_code.txt";
		write_into_file(D, params.r, nItems, ss.str().c_str());

		printf("write B and D into files done.\n");
		free(B); B=NULL;
		free(D); D=NULL;

	}
	clock_t end_of_main = clock();
	printf("total cost time %.2f\n", (double)(end_of_main - start_of_main) / CLOCKS_PER_SEC);

	CHECK_CUDA(cudaDeviceReset());
	free(UIview);
	free(IUview);
	system("pause");
	return EXIT_SUCCESS; 
}